home *** CD-ROM | disk | FTP | other *** search
- /* ——————————————————————————————————————————————————————————————————
-
- Filename: "argc Receiver.c"
- argc/argv argument receiver code, written for THINK C 5.0
- By Chris Johnson
- Version of: Thursday, July 23, 1992
-
- Distribute freely and without charge, but say something nice about
- the author when you use it. Please send me a copy of any improve-
- ments you make so they can be incorporated into future versions.
-
- ——————————————————————————————————————————————————————————————————
- Internet: chrisj@emx.utexas.edu
- UUCP: {husc6|uunet}!cs.utexas.edu!ut-emx!chrisj
- BitNet: chrisj@utxvm.bitnet
- AppleLink: chrisj@emx.utexas.edu@internet#
- CompuServe: >INTERNET:chrisj@emx.utexas.edu
- US Mail: Chris Johnson, 3311 Red River #305, Austin, TX 78705
- —————————————————————————————————————————————————————————————————— */
-
-
- #include "argc Receiver.h"
- #include <AppleEvents.h>
- #include <GestaltEqu.h>
- #include <Traps.h>
-
-
- #define argcEventClass 'args'
- #define argcEventID 'argc'
- #define argcKeyword 'argc'
- #define LBitMask(BitNum) (0x1L << (BitNum))
-
-
- static Boolean QuitFlag;
- static Boolean WaitFlag;
- static int argc;
- static char **argv;
-
-
- Boolean argInit(void);
- void argExit(void);
- void argGet(int *argc, char ***argv);
-
- Boolean EnvironmentCheck(void);
- Boolean TrapAvailable(short Trap);
-
- pascal OSErr Quit(AppleEvent *RcvdEvent, AppleEvent *ReplyEvent, long RefCon);
- pascal OSErr ArgsGet(AppleEvent *RcvdEvent, AppleEvent *ReplyEvent, long RefCon);
- void ArgsDispose(void);
- OSErr MyGotRequiredParams(AppleEvent *Event);
-
- short NumToolboxTraps(void);
- TrapType GetTrapType(short theTrap);
-
- void MacInits(void);
- Boolean BackgroundOnlyApp(void);
-
-
-
-
- #ifdef ReinvokeMain
-
- void main() {
-
- argInit();
-
- while (TRUE) {
- int argcRcvd;
- char **argvRcvd;
-
- argGet(&argcRcvd, &argvRcvd);
- mainarg(argcRcvd, argvRcvd);
- }
- }
-
- #else
-
- void argcReceiver(argcRcvd, argvRcvd)
- int *argcRcvd;
- char ***argvRcvd;
- {
- if (argInit())
- argGet(argcRcvd, argvRcvd);
- }
-
- #endif
-
-
- static Boolean argInit() {
- Boolean Continue;
-
- Continue = FALSE;
-
- MacInits();
-
- if (EnvironmentCheck())
- Continue = TRUE;
-
- return (Continue);
- }
-
-
- static void argExit() {
-
- ArgsDispose();
-
- ExitToShell();
- }
-
-
- static void argGet(argcRcvd, argvRcvd)
- int *argcRcvd;
- char ***argvRcvd;
- {
- ArgsDispose();
-
- if (QuitFlag == FALSE) {
- unsigned long TimeoutTicks;
-
- WaitFlag = TRUE;
- TimeoutTicks = (unsigned long) Ticks + 15 * 60;
-
- while (WaitFlag && ((unsigned long) Ticks < TimeoutTicks)) {
- EventRecord Event;
-
- if (WaitNextEvent(everyEvent, &Event, 60, NULL)) {
-
- switch (Event.what) {
-
- case keyDown:
-
- if ((Event.modifiers & cmdKey) != 0) {
-
- if ((unsigned char) Event.message == 'q') {
-
- QuitFlag = TRUE;
- WaitFlag = FALSE;
- }
- }
- break;
-
- case kHighLevelEvent:
- AEProcessAppleEvent(&Event);
- break;
- }
- }
- }
- }
-
- if (argv == NULL)
- argExit();
-
- *argcRcvd = argc;
- *argvRcvd = argv;
- }
-
-
- static Boolean EnvironmentCheck() {
- static Boolean Inited = FALSE;
-
- if (Inited == FALSE) {
-
- if (TrapAvailable(_WaitNextEvent)) {
-
- if (TrapAvailable(_GestaltDispatch)) {
- long Response;
-
- if (Gestalt(gestaltAppleEventsAttr, &Response) == noErr) {
-
- if ((Response & LBitMask(gestaltAppleEventsPresent)) != 0) {
-
- if (AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, Quit, 0, FALSE) == noErr) {
-
- if (AEInstallEventHandler(argcEventClass, argcEventID, ArgsGet, 0, FALSE) == noErr) {
-
- QuitFlag = FALSE;
- WaitFlag = TRUE;
-
- Inited = TRUE;
- }
- }
- }
- }
- }
- }
- }
-
- return (Inited);
- }
-
-
- static pascal OSErr Quit(RcvdEvent, ReplyEvent, RefCon)
- AppleEvent *RcvdEvent;
- AppleEvent *ReplyEvent;
- long RefCon;
- {
- OSErr OSError;
-
- OSError = MyGotRequiredParams(RcvdEvent);
- if (OSError == noErr) {
-
- QuitFlag = TRUE;
- WaitFlag = FALSE;
- }
-
- return (OSError);
- }
-
-
- static pascal OSErr ArgsGet(RcvdEvent, ReplyEvent, RefCon)
- AppleEvent *RcvdEvent;
- AppleEvent *ReplyEvent;
- long RefCon;
- {
- OSErr OSError;
- AEDesc ArgCList;
-
- OSError = AEGetParamDesc(RcvdEvent, argcKeyword, typeAEList, &ArgCList);
- if (OSError == noErr) {
- long ArgCount;
-
- OSError = AECountItems(&ArgCList, &ArgCount);
- if (OSError == noErr) {
-
- argc = ArgCount;
- argv = (char **) NewPtrClear(sizeof(Ptr) * (ArgCount + 1));
-
- OSError = MemError();
- if (OSError == noErr) {
- long Index;
- char **CurArgV;
-
- Index = 0;
- CurArgV = argv;
-
- while (--ArgCount >= 0 && OSError == noErr) {
- DescType DataType;
- long DataSize;
-
- OSError = AESizeOfNthItem(&ArgCList, ++Index, &DataType, &DataSize);
- if (OSError == noErr) {
-
- *CurArgV = (char *) NewPtr(DataSize + 1);
- OSError = MemError();
- if (OSError == noErr) {
- AEKeyword Keyword;
- long ActualSize;
-
- OSError = AEGetNthPtr(&ArgCList, Index, typeChar, &Keyword, &DataType, (Ptr) *CurArgV, DataSize, &ActualSize);
- if (OSError == noErr)
- *(*CurArgV + DataSize) = '\0';
- }
- }
-
- CurArgV++;
- }
-
- }
- }
-
- AEDisposeDesc(&ArgCList);
- }
-
- if (OSError == noErr)
- OSError = MyGotRequiredParams(RcvdEvent);
-
- if (OSError != noErr) {
-
- argc = 0;
- argv = NULL;
- }
-
- WaitFlag = FALSE;
-
- return (OSError);
- }
-
-
- static OSErr MyGotRequiredParams(Event)
- AppleEvent *Event;
- {
- OSErr OSError;
- DescType ReturnedType;
- Size ActualSize;
-
- OSError = AEGetAttributePtr(Event, keyMissedKeywordAttr, typeWildCard, &ReturnedType, NULL, 0, &ActualSize);
- if (OSError == errAEDescNotFound) {
-
- OSError = noErr;
-
- } else if (OSError == noErr)
- OSError = errAEEventNotHandled;
-
- return (OSError);
- }
-
-
- static void ArgsDispose() {
-
- if (argv != NULL) {
- char **CurArg;
-
- CurArg = argv;
-
- while (--argc >= 0) {
-
- if (*CurArg != NULL)
- DisposPtr((Ptr) *CurArg);
-
- CurArg++;
- }
-
- DisposPtr((Ptr) argv);
- }
-
- argc = 0;
- argv = NULL;
- }
-
-
- /* ——————————————————————————————————————————————————————————
- All of the following code comes from IM VI pg. 3-8.
- —————————————————————————————————————————————————————————— */
-
-
-
-
- static short NumToolboxTraps() {
- short NumToolboxTraps;
-
- if (NGetTrapAddress(_InitGraf, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
- NumToolboxTraps = 0x200;
- else
- NumToolboxTraps = 0x400;
-
- return (NumToolboxTraps);
- }
-
-
- static TrapType GetTrapType(theTrap)
- short theTrap;
- {
- TrapType tType;
-
- if ((theTrap & 0x0800) > 0)
- tType = ToolTrap;
- else
- tType = OSTrap;
-
- return (tType);
- }
-
-
- Boolean TrapAvailable(theTrap)
- short theTrap;
- {
- TrapType tType;
-
- tType = GetTrapType(theTrap);
- if (tType == ToolTrap) {
-
- theTrap &= 0x07FF;
- if (theTrap >= NumToolboxTraps())
- theTrap = _Unimplemented;
- }
-
- return (NGetTrapAddress(theTrap, tType) != NGetTrapAddress(_Unimplemented, ToolTrap));
- }
-
-
- /* ——————————————————————————————————————————————————————————
- Code to determine what sort of toolbox initializations we
- need to perform.
- —————————————————————————————————————————————————————————— */
-
-
- #define BackgroundOnlyMask 0x0400
-
-
- static void MacInits() {
- static Boolean MacInited = FALSE;
-
- if (MacInited == FALSE) {
-
- InitGraf((Ptr) &thePort);
-
- if (BackgroundOnlyApp() == FALSE) {
-
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- InitCursor();
- }
-
- MacInited = TRUE;
- }
- }
-
-
- static Boolean BackgroundOnlyApp() {
- Boolean BackgroundOnly;
- Handle SizeHand;
-
- BackgroundOnly = FALSE;
-
- SizeHand = Get1Resource('SIZE', -1);
- if (SizeHand != NULL) {
-
- LoadResource(SizeHand);
- if (ResError() == noErr) {
- short Flags;
-
- Flags = **(short **) SizeHand;
-
- if ((Flags & BackgroundOnlyMask) != 0) {
-
- BackgroundOnly = TRUE;
- }
- }
-
- ReleaseResource(SizeHand);
- }
-
- return (BackgroundOnly);
- }
-